package com.kwedding.util;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import springfox.documentation.spring.web.json.Json;

public class GsonUtil {
	/**
	 * 对象转换为json字符串
	 */
	public static String objectToJsonStr(Object obj) {
		if (obj == null)
			return null;
		Gson gson = getGson();
		return gson.toJson(obj).toString();
	}

	/**
	 * 集合转换为json字符串
	 */
	public static <T> String listToJsonStr(List<T> c) {
		if (c == null)
			return null;
		Gson gson = getGson();
		Type type = new TypeToken<List<T>>() {
		}.getType();
		return gson.toJson(c, type).toString();
	}

	/**
	 * json字符串转换为对象
	 */
	public static <T> T jsonToObject(Object object, Class<T> clazz) {
		if (object == null)
			return null;
		Gson gson = getGson();
		return gson.fromJson(object.toString(), clazz);
	}

	/**
	 * json字符串转换为对象
	 */
	public static <T> T jsonToObject(String object, Class<T> clazz) {
		if (object == null || object.length() < 1)
			return null;
		Gson gson = getGson();
		return gson.fromJson(object.toString(), clazz);
	}

	/**
	 * json字符串转换为对象集合
	 */
	public static <T> List<T> jsonToObjectList(String jsonStr, Class<T> clazz) {
		if (jsonStr == null || jsonStr.length() < 1)
			return null;
		List<T> list = new ArrayList<T>();

		JsonArray jsonArray = new JsonParser().parse(jsonStr).getAsJsonArray();

		for (int i = 0; i < jsonArray.size(); i++) {
			list.add(jsonToObject(jsonArray.get(i), clazz));
		}
		return list;
	}

	/**
	 * json字符串转换为对象集合
	 */
	public static List<String> jsonToStringList(String jsonStr) {
		if (jsonStr == null || jsonStr.length() < 1)
			return null;
		List<String> list = new ArrayList<String>();

		JsonArray jsonArray = new JsonParser().parse(jsonStr).getAsJsonArray();
		for (int i = 0; i < jsonArray.size(); i++) {
			list.add(jsonArray.get(i).toString().replaceAll("\"", ""));
		}
		return list;
	}

	/**
	 * 获取GSON
	 * 
	 * @return
	 */
	public static Gson getGson() {
		GsonBuilder builder = new GsonBuilder();
		builder.registerTypeAdapter(Date.class, new DateSerializer1()).setDateFormat(DateFormat.LONG);
		builder.registerTypeAdapter(Date.class, new DateDeserializer1()).setDateFormat(DateFormat.LONG);
		// builder.registerTypeAdapter(Json.class, new
		// NullStringToEmptyAdapterFactory<>());
		 builder.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter());
		// builder.setDateFormat(DateFormat.LONG);
		Gson gson = builder.create();
		return gson;
	}
	/*
	 * public class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory
	 * {
	 * 
	 * @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson,
	 * TypeToken<T> type) { Class<T> rawType = (Class<T>) type.getRawType(); if
	 * (rawType != String.class) { return null; } return (TypeAdapter<T>) new
	 * StringNullAdapter(); } } public class StringNullAdapter extends
	 * TypeAdapter<String> {
	 * 
	 * @Override public String read(JsonReader reader) throws IOException { // TODO
	 * Auto-generated method stub if (reader.peek() == JsonToken.NULL) {
	 * reader.nextNull(); return ""; } return reader.nextString(); }
	 * 
	 * @Override public void write(JsonWriter writer, String value) throws
	 * IOException { // TODO Auto-generated method stub if (value == null) {
	 * writer.nullValue(); return; } writer.value(value); } }
	 */

}